package com.yonyou.pmclouds.workflow.service.rmiimpl;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.dao.BaseMapper;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.task.entity.TaskConst;
import com.yonyou.pmclouds.task.entity.TaskVO;
import com.yonyou.pmclouds.task.rmiitf.TaskRemoteService;
import com.yonyou.pmclouds.workflow.entity.ApproveInfoParam;
import com.yonyou.pmclouds.workflow.entity.ProcessDefinitionVO;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import com.yonyou.pmclouds.workflow.service.rmiitf.ProcessDefinitionQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class FlowApproveImp implements IFlowApprove {

    private Map<String, IFlowApproveBuziService> typeApproveMap = new HashMap<>();
    private Map<String, BaseMapper> typeWithMapperMap = new HashMap<>();

    @Autowired
    private TaskRemoteService taskRemoteService;
    @Autowired
    private ProcessDefinitionQueryService processDefinitionQueryService;

    @Override
    public boolean approve(String pkBill, String definitionKey, byte billStatus) throws BusinessException {

        ProcessDefinitionVO definitionVO = processDefinitionQueryService.queryProcDefVOByDefiniton(definitionKey);

        if(definitionVO == null)
            throw new BusinessException("没有找到对应的流程定义信息");

        IFlowApproveBuziService flowApproveBuziService = getBusinessService(definitionVO.getBillType());

        SuperVO superVO = flowApproveBuziService.approveByPkBill(pkBill,billStatus);

        //将友工程的待办任务置为完成
        //TaskVO[] taskVOs = taskRemoteService.finishTask(pkBill, null, BillStatusConst.COMMON_APPROVED_STATUS);

        return true;
    }

    @Override
    public SuperVO approveByBillType(String pkBill, String billType, byte billStatus) throws BusinessException {
        log.debug("审批通过调用业务服务类方法：billType = " + billType + ", billStatus = " + billStatus + ", pkBill = " + pkBill);
        IFlowApproveBuziService flowApproveBuziService = getBusinessService(billType);

        log.debug("审批通过获取业务服务类： buziService = " +  (flowApproveBuziService == null ? null : flowApproveBuziService.getClass().getName()));

        if(flowApproveBuziService == null){
            log.error("审批通过获取业务服务类为空：billType = " + billType);
            throw new BusinessException("审批通过失败");
        }

        return flowApproveBuziService.approveByPkBill(pkBill, billStatus);
    }

    @Override
    public boolean reject(ApproveInfoParam approveInfoParam) throws BusinessException {

        IFlowApproveBuziService flowApproveBuziService = getBusinessService(approveInfoParam.getBillType());

        SuperVO superVO = flowApproveBuziService.rejectByPkBill(approveInfoParam.getPkBill());

        //将友工程的待办任务置为完成
        TaskVO[] taskVOs = taskRemoteService.insertTaskVOForWorkFlow(approveInfoParam, BillStatusConst.COMMON_REJECT_STATUS,null);

        //给制单人发送代办（目前支持施工记录、变更、签证、验收）
        if(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE.equals(approveInfoParam.getBillType())
                || BillTypeConst.CONSTRUCTIONLOG_BILLTYPE.equals(approveInfoParam.getBillType())
                || BillTypeConst.ALTER_AND_VISA_BILLTYPE.equals(approveInfoParam.getBillType())
                || BillTypeConst.ALTER_BILLTYPE.equals(approveInfoParam.getBillType())
                || BillTypeConst.WORKSTART_BILLTYPE.equalsIgnoreCase(approveInfoParam.getBillType())){
            taskRemoteService.insertTaskVO(genMyLaunchVOs(superVO, approveInfoParam.getUserId()));
        }
        return true;
    }

    @Override
    public SuperVO getBuziVOForWorkFlow(String pkBill , String billType) throws BusinessException{
        //根据单据类型找到对应的IFlowApproveBuziService的真实实现类
        IFlowApproveBuziService flowApproveBuziService = this.getBusinessService(billType);
        //返回单据的全部信息
        return flowApproveBuziService.queryDetailForWorkFlow(pkBill);
    }


    public void addBuziService(String billType,IFlowApproveBuziService buziService){
        log.debug("审批业务服务类注册：billType = " + billType + ", buziService = " + (buziService == null ? null : buziService.getClass().getName()));
        typeApproveMap.put(billType,buziService);
    }

    @Override
    public ProcessDefinitionVO getDefinitionByKey(String definitionKey) throws BusinessException {
        return processDefinitionQueryService.queryProcDefVOByDefiniton(definitionKey);
    }

    private TaskVO genMyLaunchVOs(SuperVO superVO, String userid) {
        TaskVO taskVO = new TaskVO();
        taskVO.setPk_bill((String) superVO.pkFiledValue());
        String pk_project = BeanHelper.getProperty(superVO, CommonFieldConst.PK_PROJECT) == null ?
                (String) BeanHelper.getProperty(superVO, CommonFieldConst.PKPROJECT) :
                (String) BeanHelper.getProperty(superVO, CommonFieldConst.PK_PROJECT);
        taskVO.setPk_project(pk_project);
        taskVO.setPk_sender(userid);
        String bill_type = BeanHelper.getProperty(superVO, CommonFieldConst.BILL_TYPE) == null ?
                (String) BeanHelper.getProperty(superVO, CommonFieldConst.BILLTYPE) :
                (String) BeanHelper.getProperty(superVO, CommonFieldConst.BILL_TYPE);

        taskVO.setSrc_bill_type(bill_type);

        taskVO.setBillstatus((byte) BeanHelper.getProperty(superVO, CommonFieldConst.BILLSTATUS));
        taskVO.setTaskcreator((String) BeanHelper.getProperty(superVO, CommonFieldConst.CREATOR));
        taskVO.setPk_receiver((String) BeanHelper.getProperty(superVO, CommonFieldConst.CREATOR));
        taskVO.setTask_status(TaskConst.TASK_TODO);
        taskVO.setLast_v_flag("Y");
        taskVO.setCreationtime(new Date(System.currentTimeMillis()));

        return taskVO;
    }

    @Override
    public SuperVO deleteBill(String pkBill, String billType) throws BusinessException {

        IFlowApproveBuziService flowApproveBuziService = this.getBusinessService(billType);

        SuperVO businessVO = flowApproveBuziService.queryDetailForWorkFlow(pkBill);

        return flowApproveBuziService.deleteBill(businessVO);
    }

    @Override
    public IFlowApproveBuziService getBusinessService(String billType) throws BusinessException {
        IFlowApproveBuziService flowApproveBuziService = typeApproveMap.get(billType);
        if(flowApproveBuziService == null)
            throw new BusinessException("根据单据类型没有找到对应的流程定义信息");
        return flowApproveBuziService;
    }

    public void addBusinessMapper(String billType, BaseMapper baseMapper){
        if(typeWithMapperMap.get(billType) == null){
            typeWithMapperMap.put(billType, baseMapper);
        }
    }

    @Override
    public Object getBusinessMapper(String billType){
        return typeWithMapperMap.get(billType);
    }
}
